from student import Student


class Student_Manage():
    # 数据仓库
    def __init__(self):
        self.student_list = []

    # 显示功能界面
    def show_menu(self):
        print("======欢迎进入学员管理系统======")
        print("\t---1、添加学员信息---")
        print("\t---2、删除学员信息---")
        print("\t---3、修改学员信息---")
        print("\t---4、查看学员信息---")
        print("\t---5、显示学员信息---")
        print("\t---6、保存学员信息---")
        print("\t-----7、退出系统-----")

        # 主函数

    # 主函数
    def run(self):
        while True:
            # 显示功能菜单
            self.show_menu()
            number = int(input("输入对应的数字实现对应功能:"))
            if number == 1:
                self.add_student()
            elif number == 2:
                self.del_student()
            elif number == 3:
                self.modify_student()
            elif number == 4:
                self.search_student()
            elif number == 5:
                self.show_student()
            elif number == 6:
                self.save_student()
            elif number == 7:
                print("欢迎下次使用")
                break

    # 添加信息
    def add_student(self):
        name = input("请输入姓名:")
        sex = input("请输入性别:")
        age = input("请输入年龄:")
        # 创建一个学生对象
        student = Student(name, sex, age)  # 实例化对象
        print(student)
        # 将创建的学生对象传入列表中
        self.student_list.append(student)

    # 删除信息
    def del_student(self):
        stu_name = input("请输入你要删除的学员姓名:")
        for data in self.student_list:
            if data.name == stu_name:
                print(data.name, "---删除成功")
                self.student_list.remove(data)
                break
        else:
            print("当前学员不存在")

    # 修改信息
    def modify_student(self):
        stu_name = input("请输入你要修改的学员姓名:")
        for data in self.student_list:
            if data.name == stu_name:
                data.name = input("请输入姓名:")
                data.sex = input("请输入性别:")
                data.age = input("请输入年龄:")
                print(f"姓名:{data.name},性别:{data.sex},年龄:{data.age}")
                break
        else:
            print("学员不存在")

    # 查找信息
    def search_student(self):
        stu_name = input("请输入你要查找的学员姓名:")
        for data in self.student_list:
            if data.name == stu_name:
                print(f"姓名:{data.name},性别:{data.sex},年龄:{data.age}")

    # 显示信息
    def show_student(self):
        for data in self.student_list:
            print(f"姓名:{data.name},性别:{data.sex},年龄:{data.age}")

    # 保存信息 重要
    def save_student(self):
        for data in self.student_list:
            data_stu = f"姓名:{data.name},性别:{data.sex},年龄:{data.age}"
            with open("student.txt", "a", encoding="utf-8") as f:
                f.write(data_stu + "\n")
